/*
** strtowordtab.c for  in /home/renard/Dropbox/epitech/dev/projets/systemunix/zappy/zappy-2015-2014s-renard_g/serv-src
**
** Made by guillaume renard
** Login   <renard_g@epitech.net>
**
** Started on  Wed Jun 13 08:43:38 2012 guillaume renard
** Last update Thu Jul 19 18:09:20 2012 pierrick druart
*/

#include	<string.h>
#include	<stdlib.h>

#include	"server.h"

static int	comptLen(const char *line, const char del)
{
  int		i;
  int		len;

  len = 1;
  i = 0;
  while (line[i])
  {
    if (line[i] == del)
      len++;
    i++;
  }
  if (len > 2)
    len = 2;
  return (len);
}

static char	*savLine(char *tab, const char *line, int *i)
{
  int		y;

  y = 0;
  while (line[*i] != ' ' && line[*i] != '\n' && line[*i] != '\0')
  {
    tab[y] = line[*i];
    y++;
    (*i)++;
  }
  (*i)++;
  return (tab);
}

char		**getTab(const char *line, const char del)
{
  char		**tab;
  int	       	i;
  int	       	len;
  int	       	x;

  x = 0;
  len = comptLen(line, del);
  tab = xcalloc(len + 1, sizeof(char *));
  i = 0;
  while (x < len)
  {
    tab[x] = xcalloc(strlen(line), sizeof(char));
    tab[x] = savLine(tab[x], line, &i);
    x++;
  }
  tab[x] = NULL;
  return (tab);
}

void		freetab(char **tab)
{
  int		i;

  i = 0;
  while (tab[i])
  {
    free(tab[i]);
    i++;
  }
  free(tab);
}
